package com.rimi.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rimi.bean.User;
import com.rimi.cache.SelectCache;
import com.rimi.service.UserService;
import com.rimi.util.DecriptUtil;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/miss")
public class PasswordController {

    @Autowired
    private UserService userService;

    @Autowired
    private SelectCache selectCache;

    //修改密码
    @RequestMapping("/modifyPassword")
    public String modifyPassword(ModelMap model, HttpServletResponse response, HttpSession session, String isok, String oldps, String ps, String newps) {
        User user = (User) session.getAttribute("user");
        model.addAttribute("user1",user);
        if("1".equals(isok)){
            System.out.println("654645654654");
            if(ps.equals(newps)){
                if(user.getUserPs().equals(DecriptUtil.MD5(oldps))){
                    User user1 = new User();
                    user1.setUserPs(DecriptUtil.MD5(ps));
                    boolean hehe = userService.update(user1, new EntityWrapper<User>().eq("user_id", user.getUserId()));
                    selectCache.removeCacheList("user"+user.getUserName());
                    selectCache.removeCacheList("user"+user.getUserPhone());
                    user.setUserPs(DecriptUtil.MD5(ps));
                    session.setAttribute("user",user);
                    if(hehe){

                        String json = JSON.toJSONString("ok");
                        try {
                            response.getWriter().write(json);
                            response.getWriter().close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }else{

                        String json = JSON.toJSONString("no");
                        try {
                            response.getWriter().write(json);
                            response.getWriter().close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }

                }else {
                    String json = JSON.toJSONString("oldpsno");
                    try {
                        response.getWriter().write(json);
                        response.getWriter().close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }else{
                String json = JSON.toJSONString("ps");
                try {
                    response.getWriter().write(json);
                    response.getWriter().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }


        return "account_security";
    }
}
